package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.budwk.app.sqfw.models.Sqfw_rally;
import com.budwk.app.sqfw.services.SqfwRallyForAdminService;
import com.budwk.app.sqfw.services.SqfwRallyService;
import com.budwk.app.sqfw.services.SqfwRallySignForAdminService;
import com.budwk.app.sqfw.vo.SqfwCustomForAdminCommonVO;
import com.budwk.app.sqfw.vo.SqfwRallyVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwV2.dto.TemplateFileDTO;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: wk-mini-dev-env
 * @description: 社区活动（后台管理系统）
 * @author: LJT
 * @create: 2023-11-14 09:56
 **/
@IocBean
@At("/platform/sqfw/sqfwRally")
@ApiDefinition(tag = "社区活动（后台管理系统）")
@Slf4j
public class SqfwRallyForAdminController {

    @Inject
    private SqfwRallyForAdminService sqfwRallyForAdminService;

    @Inject
    private SqfwRallySignForAdminService sqfwRallySignForAdminService;

    @Inject
    private SqfwRallyService sqfwRallyService;

    /**
     * 社区活动列表查询（用于后台管理系统）
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "社区活动列表查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = SqfwRallyVO.class
    )
    @ApiResponses(
            implementation = SqfwRallyVO.class
    )
    @SLog(tag = "社区活动列表查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> list(@Param("..") SqfwRallyVO bean) {
        Cnd cnd = sqfwRallyForAdminService.buildConditionForQueryToCnd(bean);
        Pagination pagination = sqfwRallyForAdminService.listPage(bean.getPageNo(), bean.getPageSize(), cnd);
        List<Sqfw_rally> rallyList = pagination.getList(Sqfw_rally.class);
        List<String> rallyIdList = rallyList.stream().map(Sqfw_rally::getId).collect(Collectors.toList());
        Map<String, String> pdfUrlMap = sqfwRallySignForAdminService.queryRallyReportsPdfColl(bean, rallyIdList);
        List<SqfwRallyVO> finalSqfwRallyList = new ArrayList<SqfwRallyVO>();
        for (Sqfw_rally obj : rallyList) {
            SqfwRallyVO finalSqfwRally = new SqfwRallyVO();
            BeanUtil.copyProperties(obj, finalSqfwRally);
            for (String key : pdfUrlMap.keySet()) {
                if (key.equals(finalSqfwRally.getId())) {
                    finalSqfwRally.setRallypdfUrl(pdfUrlMap.get(key));
                }
            }
            finalSqfwRallyList.add(finalSqfwRally);
        }
        pagination.setList(finalSqfwRallyList);
        return Result.success(pagination);
    }

    /**
     * 社区活动详情查询（用于后台管理系统）
     */
    @At("/detail")
    @Ok("json")
    @POST
    @ApiOperation(name = "社区活动详情查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "id", example = "11111", description = "id", type = "String"),
            }
    )
    @ApiResponses(
            implementation = Sqfw_rally.class
    )
    @SLog(tag = "社区活动详情查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> detail(@Param("id") String id) {
        if (Strings.isBlank(id)) {
            return Result.error("请求参数不能为空！");
        }
        Sqfw_rally sqfwRally = sqfwRallyForAdminService.fetch(id);
        return Result.success(sqfwRally);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除表数据")
    @SLog(tag = "删除表数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id) {
        Sqfw_rally sqfwRally = sqfwRallyForAdminService.fetch(id);
        if (null == sqfwRally) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwRallyForAdminService.deleteRally(id);
        return Result.success();
    }

    /**
     * 文件下载
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "文件下载")
    @SLog(tag = "文件下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downloadFile(@Param("..") SqfwRallyVO bean, @Param("ids") List<String> ids) {
        return Result.data(sqfwRallyForAdminService.downloadFile(bean, ids));
    }

    /**
     * 是否存在下载数据查询
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "是否存在下载数据查询")
    @SLog(tag = "是否存在下载数据查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> checkDownloadData(@Param("..") SqfwRallyVO bean, @Param("ids") List<String> ids) {
        return Result.data(sqfwRallyForAdminService.checkDowloadData(bean, ids));
    }
}
